package com.example.kliven.because.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

/**
 * Created by Kliven on 16/1/15.
 */
public class SQList {

    private Context context;
    private MySQHelper mySQHelper;
    private SQLiteDatabase sqLiteDatabase;

    public SQList(Context context) {
        this.context = context;
        mySQHelper = new MySQHelper(context, "UserInfo.db", null, 1);
        sqLiteDatabase = mySQHelper.getWritableDatabase();
    }

    private boolean hasTest(String text) {
        boolean b;
        String sql = "select * from userInfo where name = ?";
        Cursor cursor = sqLiteDatabase.rawQuery(sql, new String[]{text});
        b = cursor.getCount() > 0;
        cursor.close();
        return b;
    }

    private void insertData(String username, String email, String password) {
        if (username.length() < 1) {
            Toast.makeText(context, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        } else if (email.length() < 1) {
            Toast.makeText(context, "邮箱不能为空", Toast.LENGTH_SHORT).show();
            return;
        } else if (password.length() < 1) {
            Toast.makeText(context, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        } else if (hasTest(username)) {
            Toast.makeText(context, "用户名已存在", Toast.LENGTH_SHORT).show();
        } else {
            ContentValues values = new ContentValues();
            values.put("username", username);
            values.put("email", email);
            values.put("password", password);
            sqLiteDatabase.insert("userInfo", null, values);
        }
    }

    private Boolean Query(String table, String username, String password) {
        Cursor cursor = sqLiteDatabase.rawQuery("select * from userInfo", null);
        int i = 0;
        while (cursor.moveToNext()) {
            i++;
            if (username.equals(cursor.getString(1)) && password.equals(cursor.getString(2))) {
                return true;
            }
        }
        cursor.close();
        return false;
    }

//    boolean judge = false;
//    String nameInput ="";
//    String mimaInput ="";
//    nameInput = yonghuEdit.getText().toString();
//    mimaInput = mimaEdit.getText().toString();
//    judge = sql.Query("client",nameInput,mimaInput);
//    if(judge == true){
//        Intent intent = new Intent();
//        intent.setClass(Yonghurenzheng.this,Fabuxinxi.class);  //跳转到下一界面，下一界面是fabuxinxi.class 不贴啦
//        startActivity(intent);
//    }
//    else{
//        Toast.makeText(Yonghurenzheng.this, "您的用户名和密码不正确", Toast.LENGTH_LONG).show();
//    }
}
